package cn.iocoder.yudao.module.blog.controller.app.user;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.blog.controller.app.user.vo.AppUserInfoRespVO;
import cn.iocoder.yudao.module.blog.convert.user.UserConvert;
import cn.iocoder.yudao.module.blog.dal.dataobject.follow.BlogFollowDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiucase.BlogJiuCaseDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.post.BlogPostDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.space.BlogSpaceDO;
import cn.iocoder.yudao.module.blog.dal.mysql.follow.BlogFollowMapper;
import cn.iocoder.yudao.module.blog.dal.mysql.jiucase.BlogJiuCaseMapper;
import cn.iocoder.yudao.module.blog.dal.mysql.post.BlogPostMapper;
import cn.iocoder.yudao.module.blog.dal.mysql.space.BlogSpaceMapper;

import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.level.MemberLevelService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.USER_NOT_EXISTS;

@Tag(name = "用户 APP - 用户喜喜")
@RestController
@RequestMapping("/blog/user")
@Validated
public class AppUserController {
    @Resource
    private MemberUserService userService;
    @Resource
    private MemberLevelService levelService;

    @Resource
    private BlogJiuCaseMapper blogJiuCaseMapper;

    @Resource
    private BlogSpaceMapper blogSpaceMapper;

    @Resource
    private BlogPostMapper blogPostMapper;

    @Resource
    private BlogFollowMapper blogFollowMapper;

    @GetMapping("/get")
    @Operation(summary = "查看玩家信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppUserInfoRespVO> getUserInfo(@RequestParam("id") Long id) {
        MemberUserDO user = userService.getUser(id);

        if(user == null){
            return success(null);
        }

        MemberLevelDO level = levelService.getLevel(user.getLevelId());

        AppUserInfoRespVO appUserInfoRespVO = UserConvert.INSTANCE.convert(user, level);

        Long caseCount = blogJiuCaseMapper.selectCount(BlogJiuCaseDO::getMemberId, id);
        appUserInfoRespVO.setCaseCount(caseCount);

        Long postCount = blogPostMapper.selectCount(BlogPostDO::getMemberId, id);
        appUserInfoRespVO.setPostCount(postCount);

        Long spaceCount = blogSpaceMapper.selectCount(BlogSpaceDO::getMemberId, id);
        appUserInfoRespVO.setSpaceCount(spaceCount);

        Long fansCount = blogFollowMapper.selectCount(BlogFollowDO::getFollowMemberId, id);
        appUserInfoRespVO.setFansCount(fansCount);

        return success(appUserInfoRespVO);
    }

    @GetMapping("/get-by-mobile")
    @Operation(summary = "查看玩家信息")
    @Parameter(name = "mobile", description = "编号", required = true, example = "1024")
    public CommonResult<AppUserInfoRespVO> getUserInfo(@RequestParam("mobile") String mobile) {
        MemberUserDO user = userService.getUserByMobile(mobile);

        MemberLevelDO level = levelService.getLevel(user.getLevelId());

        AppUserInfoRespVO appUserInfoRespVO = UserConvert.INSTANCE.convert(user, level);

        Long caseCount = blogJiuCaseMapper.selectCount(BlogJiuCaseDO::getMemberId, user.getId());
        appUserInfoRespVO.setCaseCount(caseCount);

        Long postCount = blogPostMapper.selectCount(BlogPostDO::getMemberId, user.getId());
        appUserInfoRespVO.setPostCount(postCount);

        Long spaceCount = blogSpaceMapper.selectCount(BlogSpaceDO::getMemberId, user.getId());
        appUserInfoRespVO.setSpaceCount(spaceCount);

        Long fansCount = blogFollowMapper.selectCount(BlogFollowDO::getFollowMemberId, user.getId());
        appUserInfoRespVO.setFansCount(fansCount);

        return success(appUserInfoRespVO);
    }
}
